#include "vector.h"
#include<vector>

template <class T>
void print(const cyq::vector<T>& v)
{
	//不能从没有实例化的类里面取东西
	//编译器会分不清const_iterator 是类型还是静态成员变量
	//使用typename就可以解决
	typename cyq::vector<T>::const_iterator it = v.begin();

	while (it != v.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	for (auto e : v)
	{
		cout << e << " ";
	}
}

void test_vector1()
{
	cyq::vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(5);
	v1.push_back(6);

	for (auto& e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	for (size_t i = 0; i < v1.size(); i++)
	{
		v1[i]++;
	}
	for (size_t i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl;

	print(v1);
	cout << endl;
}

void test_vector2()
{
	cyq::vector<int> v2;
	v2.push_back(1);
	v2.push_back(2);
	v2.push_back(3);
	v2.push_back(4);
	v2.push_back(5);
	v2.push_back(6);
	v2.push_back(7);

	//使用insert后就不要使用这个形参的迭代器了，因为它可能会失效
	v2.insert(v2.begin() + 3, 100);
	v2.erase(v2.begin() + 3);
	for (auto& e : v2)
	{
		cout << e << " ";
	}
	cout << endl;

	//it迭代器会失效
	//可以使用更新后的位置--需要接收返回值
	auto it = v2.begin()+3;
	it=v2.erase(it);
	cout << *it << endl;
	(*it)++;
	cout << *it << endl;

}

//vs下erase的迭代器失效问题--需要返回值
//删除所有的偶数
void test_vector3()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(6);

	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		if (*it % 2 == 0)
		{
			it = v.erase(it);
		}
		else
		{
			it++;
		}
	}

	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;

}

//vector模拟实现下erase的迭代器失效问题--需要返回值
void test_vector4()
{
	cyq::vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(6);

	cyq::vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		//报错
		//if (*it % 2 == 0)
		//{
		//	 v.erase(it);
		//}
		//	it++;
		//

		if (*it % 2 == 0)
		{
			 it=v.erase(it);
		}
		else
		{
			it++;
		}
	}

	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;

}

void test_vector5()
{
	cyq::vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(6);

	v.pop_back();
	v.pop_back();

	v.resize(10, 0);

	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_vector6()
{
	cyq::vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(6);

	cyq::vector<int> v1(v);
	for (auto& e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	cyq::vector<int> v2;
	v2.resize(10, 0);
	v1 = v2;
	for (auto& e : v1)
	{
		cout << e << " ";
	}
	cout << endl;


}

void test_vector7()
{
	//vector是深拷贝 vector数组里面存的是string对象
	//string对象使用memcpy会导致浅拷贝--使用同一块空间 delete会造成野指针
	//解决方案：T是string这样需要深拷贝的类 就调用string的赋值重载来实现string对象的深拷贝（一样适用内置类型--简单的值拷贝）
	vector<string> v;
	v.push_back("1111111111111111");
	v.push_back("2222222222222222");
	v.push_back("3333333333333333");
	v.push_back("4444444444444444");
	v.push_back("5555555555555555");
	v.push_back("6666666666666666");

	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	vector<string> v1(v);

	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;



}

void test_vector8()
{
	vector<int> v(10, 1);
	vector<string> v1(10, "1111");
	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;
	for (auto& e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	string s1("hello");
	vector<char> v2(s1.begin(), s1.end());
	for (auto& e : v2)
	{
		cout << e << " ";
	}
	cout << endl;

	int a[] = { 0,1,2,3,4,5,6,7,8,9 };
	vector<int> v3(a, a + sizeof(a) / sizeof(a[0]));
	for (auto& e : v3)
	{
		cout << e << " ";
	}
	cout << endl;
}


int main()
{
	test_vector1();
	cout << "---------------------------" << endl;

	test_vector2();
	cout << "---------------------------" << endl;

	test_vector3();
	cout << "---------------------------" << endl;

	test_vector4();
	cout << "---------------------------" << endl;

	test_vector5();
	cout << "---------------------------" << endl;

	test_vector6();
	cout << "---------------------------" << endl;

	test_vector7();
	cout << "---------------------------" << endl;

	test_vector8();
	cout << "---------------------------" << endl;


	return 0;

}